<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>python语法及编程思想 | 如是的学习博客</title><meta name="keywords" content="python"><meta name="author" content="rushiy"><meta name="copyright" content="rushiy"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="该章节基础知识借鉴于网络，主要参考https:&#x2F;&#x2F;edu.csdn.net&#x2F;skill&#x2F;python&#x2F;python-3-1?category&#x3D;1&amp;typeId&#x3D;17334和https:&#x2F;&#x2F;gitbook.cn&#x2F;gitchat&#x2F;column&#x2F;5e37978dec8d9033cf916b5d&#x2F;topic&#x2F;5e3a664bec8d9033cf920db2两篇文档，若侵权，请联系删除  四大基本">
<meta property="og:type" content="article">
<meta property="og:title" content="python语法及编程思想">
<meta property="og:url" content="https://rushiy.gitee.io/python/python%E8%AF%AD%E6%B3%95%E5%8F%8A%E7%BC%96%E7%A8%8B%E6%80%9D%E6%83%B3/index.html">
<meta property="og:site_name" content="如是的学习博客">
<meta property="og:description" content="该章节基础知识借鉴于网络，主要参考https:&#x2F;&#x2F;edu.csdn.net&#x2F;skill&#x2F;python&#x2F;python-3-1?category&#x3D;1&amp;typeId&#x3D;17334和https:&#x2F;&#x2F;gitbook.cn&#x2F;gitchat&#x2F;column&#x2F;5e37978dec8d9033cf916b5d&#x2F;topic&#x2F;5e3a664bec8d9033cf920db2两篇文档，若侵权，请联系删除  四大基本">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-10-17T08:00:14.000Z">
<meta property="article:author" content="rushiy">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary"><link rel="shortcut icon" href="/img/favicon.jpg"><link rel="canonical" href="https://rushiy.gitee.io/python/python%E8%AF%AD%E6%B3%95%E5%8F%8A%E7%BC%96%E7%A8%8B%E6%80%9D%E6%83%B3/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'python语法及编程思想',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-19 01:18:40'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/favicon.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">如是的学习博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">python语法及编程思想</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-10-17T08:00:14.000Z" title="发表于 2022-10-17 16:00:14">2022-10-17</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-18T17:18:40.805Z" title="更新于 2022-11-19 01:18:40">2022-11-19</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="python语法及编程思想"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>该章节基础知识借鉴于网络，主要参考<a target="_blank" rel="noopener" href="https://edu.csdn.net/skill/python/python-3-1?category=1&amp;typeId=17334%E5%92%8Chttps://gitbook.cn/gitchat/column/5e37978dec8d9033cf916b5d/topic/5e3a664bec8d9033cf920db2%E4%B8%A4%E7%AF%87%E6%96%87%E6%A1%A3%EF%BC%8C%E8%8B%A5%E4%BE%B5%E6%9D%83%EF%BC%8C%E8%AF%B7%E8%81%94%E7%B3%BB%E5%88%A0%E9%99%A4">https://edu.csdn.net/skill/python/python-3-1?category=1&amp;typeId=17334和https://gitbook.cn/gitchat/column/5e37978dec8d9033cf916b5d/topic/5e3a664bec8d9033cf920db2两篇文档，若侵权，请联系删除</a></p>
</blockquote>
<h2 id="四大基本语法"><a href="#四大基本语法" class="headerlink" title="四大基本语法"></a>四大基本语法</h2><p>分别从变量命名规则、缩进原则、特殊关键字和特殊运算符四个方面，总结 Python 的基本语法。</p>
<h3 id="命名规则"><a href="#命名规则" class="headerlink" title="命名规则"></a>命名规则</h3><p>Python的变量命名规则主要包括两条：</p>
<ul>
<li>允许包括英文、数字以及下划线（<code>_</code>），不能以数字开头</li>
<li>名称区分大小写</li>
</ul>
<p>特别说明以“下划线”开头的变量是有特殊意义的：</p>
<ul>
<li>类变量若以单下划线（<code>_</code>）开头，代表不能直接被访问，类似于 C# 的受保护型变量（protected），表示不能通过 <code>import module_name</code> 而导入。</li>
<li>类变量若以双下划（<code>__</code>）开头，表示为类的私有成员，不能被导入和其他类变量访问。</li>
<li>以双下划开头和双下划线结尾的变量是 Python 里的专用标识，有特殊的身份。</li>
</ul>
<p>如 Python 自定义类中都包括 <strong>init</strong> 和 <strong>add</strong> 方法，如果不重写 <strong>add</strong> 去执行两个类加法操作，程序会抛 TypeError 异常。只有重写后，程序才能正常执行加法操作。</p>
<p>Python 变量命名习惯一般遵守蛇形命名法（snake case）：</p>
<ul>
<li>一般变量命名，book_id、book_store_count；</li>
<li>类名首字符为大写，如 Python 内置模块 collections.abc 中的 Iterable 类、我们自定义的 Book 类等；</li>
<li>类方法名：get_store_count()；</li>
<li>其他特殊变量，会全部大写，M_PI、MAX_VEHICLE_SPEED。</li>
</ul>
<p>这与 Java 命名方法不同，Java最典型的命名方法——驼峰命名法（camel case）。</p>
<h3 id="缩进原则"><a href="#缩进原则" class="headerlink" title="缩进原则"></a>缩进原则</h3><p>Python最具特色的地方就是用缩进代替 Java、C++ 中的 <code>&#123;&#125;</code>，缩进的层级结构表示代码的逻辑层次。</p>
<p>比如，自定义一个 Book 类，重写 <strong>add</strong> 方法计算两类书的库存量和。</p>
<p>Python 的缩进方法，一般为 4 个字符。</p>
<ul>
<li>代码行 <code>class Book(object)</code> 与代码行 <code># 定义类的参数</code> 的缩进，此处为 4 个字符；</li>
<li>代码行 <code>def __add__(self,book):</code> 与 <code>return</code> 所在行缩进也是 4 个字符。</li>
</ul>
<p>通过这种层级结构，展现代码的逻辑层次。</p>
<p>下面代码，创建一个 Book 类：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Book</span>(<span class="title class_ inherited__">object</span>):</span><br><span class="line">    <span class="comment"># 定义类的参数</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self,book_id, book_name, book_store_count</span>):</span><br><span class="line">        self.book_id = book_id</span><br><span class="line">        self.book_name = book_name</span><br><span class="line">        self.book_store_count = book_store_count</span><br><span class="line">    <span class="comment"># 重写加法操作    </span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__add__</span>(<span class="params">self,book</span>):</span><br><span class="line">        <span class="keyword">return</span> self.book_store_count + book.book_store_count</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建两个 Book 类的实例：</span></span><br><span class="line">python_intro_book = Book(<span class="number">1</span>,<span class="string">&#x27;python入门书&#x27;</span>,<span class="number">100</span>) </span><br><span class="line">ml_intro_book = Book(<span class="number">2</span>,<span class="string">&#x27;机器学习入门书&#x27;</span>,<span class="number">200</span>)</span><br><span class="line"><span class="comment"># 求两本书的总销量</span></span><br><span class="line">sales_cnt = python_intro_book + ml_intro_book</span><br><span class="line"><span class="built_in">print</span>(sales_cnt) <span class="comment"># 300</span></span><br></pre></td></tr></table></figure>

<p>如下是代码执行结果的演示图，打印总销量 300。结合图形，辅助大家快速理解代码。</p>
<p><img src="/pic/python-pic/d20cbcc0-51fc-11ea-b315-dd4a0f63c225.png" alt="image-20200218101020064"></p>
<p>为了帮助新手更容易理解代码整个执行过程，专栏会配备相应的演示动画：</p>
<p><img src="/pic/python-pic/77e2e910-877e-11ea-a9ff-29e53f17413b.gif" alt="img"></p>
<p>缩进格式、行间空行数、变量和等号空格等 Python 编码规范参考PEP8。</p>
<p>autopep8包遵循 PEP8 的所有规范，安装此包，做好相关配置，便可自动实现 PEP8 制定的编码规范。</p>
<h3 id="特殊关键字"><a href="#特殊关键字" class="headerlink" title="特殊关键字"></a>特殊关键字</h3><p>Python 有 35 个关键字：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">False</span>      <span class="keyword">await</span>      <span class="keyword">else</span>       <span class="keyword">import</span>     <span class="keyword">pass</span></span><br><span class="line"><span class="literal">None</span>       <span class="keyword">break</span>      <span class="keyword">except</span>     <span class="keyword">in</span>         <span class="keyword">raise</span></span><br><span class="line"><span class="literal">True</span>       <span class="keyword">class</span>      <span class="title class_">finally</span>    <span class="keyword">is</span>         <span class="keyword">return</span></span><br><span class="line"><span class="keyword">and</span>        <span class="keyword">continue</span>   <span class="keyword">for</span>        <span class="keyword">lambda</span>     <span class="keyword">try</span></span><br><span class="line"><span class="keyword">as</span>         <span class="keyword">def</span>        <span class="title function_">from</span>       <span class="keyword">nonlocal</span>   <span class="keyword">while</span></span><br><span class="line"><span class="keyword">assert</span>     <span class="keyword">del</span>        <span class="keyword">global</span>     <span class="keyword">not</span>        <span class="keyword">with</span></span><br><span class="line"><span class="keyword">async</span>      <span class="keyword">elif</span>       <span class="keyword">if</span>         <span class="keyword">or</span>         <span class="keyword">yield</span></span><br></pre></td></tr></table></figure>

<p>自定义变量名不能与它们重复。</p>
<p>常用且不同于其他常用语言 C++ 和 Java 的关键字，如：</p>
<ul>
<li><code>True</code> 和 <code>False</code> 用于表示值的真假，在 Java 中是 <code>true</code> 和 <code>false</code>；</li>
<li>逻辑反操作 Python 使用 <code>not</code>，Java 是 <code>!</code>；</li>
<li><code>None</code> 表示空值，Java 使用 <code>null</code>；</li>
<li>Python 两个条件同时满足使用 <code>and</code>，Java 是 <code>&amp;&amp;</code>；</li>
<li>两者满足其一，Python 使用 <code>or</code>，Java 使用 <code>||</code>；</li>
<li>Python 使用 <code>elif</code>， Java 是 <code>else if</code>。</li>
</ul>
<p>其他比较特殊的关键字，如：</p>
<ul>
<li><code>del</code> 用于删除可迭代对象中某个元素；</li>
<li><code>def</code> 用于定义函数；</li>
<li>带 <code>yield</code> 用于定义生成器（generator）函数；</li>
<li><code>global</code> 和 <code>nonlocal</code> 一种应用是 Python 函数式编程的闭包场景；</li>
<li><code>pass</code> 一种应用是定义接口，也是 Python 语言特有的一个关键字。</li>
</ul>
<p>这些关键字的用法，会在后续文章，更为详细的介绍。在此，先构建一个整体上的认识即可。</p>
<h3 id="特殊运算符"><a href="#特殊运算符" class="headerlink" title="特殊运算符"></a>特殊运算符</h3><p>Python 的运算符包括：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">+       -       *       **      /       //      %      @</span><br><span class="line">&lt;&lt;      &gt;&gt;      &amp;       |       ^       ~       :=</span><br><span class="line">&lt;       &gt;       &lt;=      &gt;=      ==      !=</span><br></pre></td></tr></table></figure>

<p>大部分运算符应该被熟知，重点介绍 3 个比较特殊的：<code>//</code>、<code>**</code>、<code>:=</code>。</p>
<p><code>//</code> 用于两个数值相除且向下取整，与 Python 的 math 模块中 floor 功能相似：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">1</span>]: <span class="number">5</span>//<span class="number">2</span></span><br><span class="line">Out[<span class="number">1</span>]: <span class="number">2</span></span><br><span class="line">In [<span class="number">2</span>]: <span class="number">5</span>//<span class="number">4.5</span></span><br><span class="line">Out[<span class="number">2</span>]: <span class="number">1.0</span></span><br></pre></td></tr></table></figure>

<p><code>**</code> 用于幂运算：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">1</span>]: <span class="number">2</span>**<span class="number">3</span></span><br><span class="line">Out[<span class="number">1</span>]: <span class="number">8</span></span><br></pre></td></tr></table></figure>

<p><code>:=</code> 是在 2019 年，Python 3.8 版本里，刚刚才被支持的运算符，被形象地称为“海象运算符”。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">n = <span class="built_in">len</span>(a)</span><br><span class="line"><span class="keyword">if</span> n &gt; <span class="number">10</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;<span class="subst">&#123;n&#125;</span>大于10&quot;</span>)</span><br></pre></td></tr></table></figure>

<p>如果使用“海象运算符”，写法上更为精简：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (n := <span class="built_in">len</span>(a)) &gt; <span class="number">10</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;<span class="subst">&#123;n&#125;</span>大于10&quot;</span>)</span><br></pre></td></tr></table></figure>

<p>Python 比较运算符还支持链式比较，应用起来更加方便，比如：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">i = <span class="number">3</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">1</span> &lt; i &lt; <span class="number">3</span>) <span class="comment"># False</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">1</span> &lt; i &lt;= <span class="number">3</span>) <span class="comment"># True</span></span><br></pre></td></tr></table></figure>

<h2 id="pyhon编程思想"><a href="#pyhon编程思想" class="headerlink" title="pyhon编程思想"></a>pyhon编程思想</h2><p>Python是一种面向对象<code>oop(Object Oriented Programming)</code>的脚本语言。</p>
<p><code>面向对象是采用基于对象（实体）的概念建立模型，模拟客观世界分析、设计、实现软件的办法</code>。</p>
<p>在面向对象程序设计中，对象包含两个含义，其中一个是<code>数据</code>，另外一个是<code>动作</code>。面向对象的方法把数据和方法组合成一个整体，然后对其进行系统建模。</p>
<blockquote>
<p>python编程思想的核心就是<strong>理解功能逻辑</strong>，如果对解决一个问题的逻辑没有搞清楚，那么你的代码看起来就会非常的紊乱，读起来非常的拗口，所以一旦逻辑清晰，按照模块对功能进行系统编程，那么你的代码设计肯定是漂亮的！！！</p>
</blockquote>
<h3 id="基本的程序设计模式"><a href="#基本的程序设计模式" class="headerlink" title="基本的程序设计模式"></a>基本的程序设计模式</h3><p>任何的程序设计都包含<code>IPO</code>，它们分别代表如下：</p>
<ul>
<li>I：Input 输入，程序的输入</li>
<li>P：Process 处理，程序的主要逻辑过程</li>
<li>O：Output 输出，程序的输出</li>
</ul>
<p>因此如果想要通过计算机实现某个功能，那么<strong>基本的程序设计模式</strong>包含三个部分，如下：</p>
<ul>
<li>确定IPO：<strong>明确需要实现功能的输入和输出，以及主要的实现逻辑过程</strong>；</li>
<li>编写程序：将计算求解的逻辑过程通过编程语言进行设计展示；</li>
<li>调试程序：对编写的程序按照逻辑过程进行调试，确保程序按照正确逻辑正确运行。</li>
</ul>
<h3 id="解决复杂问题的有效方法：自顶向下（设计）"><a href="#解决复杂问题的有效方法：自顶向下（设计）" class="headerlink" title="解决复杂问题的有效方法：自顶向下（设计）"></a>解决复杂问题的有效方法：自顶向下（设计）</h3><h4 id="自顶向下-分而治之"><a href="#自顶向下-分而治之" class="headerlink" title="自顶向下-分而治之"></a>自顶向下-分而治之</h4><p>如果要实现功能的逻辑比较复杂的时候，就需要对其进行<strong>模块化设计</strong>，将复杂问题进行分解，转化为多个简单问题，其中简单问题又可以继续分解为更加简单的问题，直到功能逻辑可以通过模块程序设计实现，这也是程序设计的<strong>自顶向下</strong>特点。总结如下：</p>
<ul>
<li>将一个总问题表达为若干个小问题组成的形式</li>
<li>使用同样方法进一步分解小问题</li>
<li>直至，小问题可以用计算机简单明了的解决</li>
</ul>
<h4 id="案例1：体育竞技分析"><a href="#案例1：体育竞技分析" class="headerlink" title="案例1：体育竞技分析"></a>案例1：体育竞技分析</h4><p>（1）程序总体框架</p>
<blockquote>
<p>printlnfo()              步骤1:打印程序的介绍性信息<br>    getlnputs()             步骤2:获得程序运行参数：proA, proB, ···, pron<br>    simNGames()        步骤3:利用球员A和B的能力值，模拟n局比赛<br>    printSummary()   步骤4:输出球员A和B获胜比赛的场次及概率     </p>
</blockquote>
<p>（2）程序设计</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 导入python资源包</span></span><br><span class="line"><span class="keyword">from</span> random <span class="keyword">import</span> random</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 用户体验模块 </span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">printIntro</span>():</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;这个程序模拟两个选手A和B的某种竞技比赛&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;程序运行需要A和B的能力值（以0到1之间的小数表示）&quot;</span>)</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 获得A和B的能力值与场次模块 </span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">getIntputs</span>():</span><br><span class="line">    a = <span class="built_in">eval</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入A的能力值（0-1）：&quot;</span>))</span><br><span class="line">    b = <span class="built_in">eval</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入B的能力值（0-1）：&quot;</span>))</span><br><span class="line">    n = <span class="built_in">eval</span>(<span class="built_in">input</span>(<span class="string">&quot;模拟比赛的场次：&quot;</span>))</span><br><span class="line">    <span class="keyword">return</span> a, b, n</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 模拟n局比赛模块 </span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">simNGames</span>(<span class="params">n, probA, probB</span>):</span><br><span class="line">    winsA, winsB = <span class="number">0</span>, <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line">        scoreA, scoreB = simOneGame(probA, probB)</span><br><span class="line">        <span class="keyword">if</span> scoreA &gt; scoreB:</span><br><span class="line">            winsA += <span class="number">1</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            winsB += <span class="number">1</span></span><br><span class="line">    <span class="keyword">return</span> winsA, winsB</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 判断比赛结束条件 </span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">gameOver</span>(<span class="params">a, b</span>):</span><br><span class="line">    <span class="keyword">return</span> a == <span class="number">15</span> <span class="keyword">or</span> b == <span class="number">15</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># 模拟n次单局比赛=模拟n局比赛 </span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">simOneGame</span>(<span class="params">probA, probB</span>):</span><br><span class="line">    scoreA, scoreB = <span class="number">0</span>, <span class="number">0</span></span><br><span class="line">    serving = <span class="string">&quot;A&quot;</span></span><br><span class="line">    <span class="keyword">while</span> <span class="keyword">not</span> gameOver(scoreA, scoreB):</span><br><span class="line">        <span class="keyword">if</span> serving == <span class="string">&quot;A&quot;</span>:</span><br><span class="line">            <span class="keyword">if</span> random() &lt; probA:</span><br><span class="line">                scoreA += <span class="number">1</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                serving = <span class="string">&quot;B&quot;</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">if</span> random() &lt; probB:</span><br><span class="line">                scoreB += <span class="number">1</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                serving = <span class="string">&quot;A&quot;</span></span><br><span class="line">    <span class="keyword">return</span> scoreA, scoreB</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 打印结果模块 </span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">printSummary</span>(<span class="params">winsA, winsB</span>):</span><br><span class="line">    n = winsA + winsB</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;竞技分析开始，共模拟&#123;&#125;场比赛&quot;</span>.<span class="built_in">format</span>(n))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;选手A获胜&#123;&#125;场比赛，占比&#123;:0.1%&#125;&quot;</span>.<span class="built_in">format</span>(winsA, winsA / n))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;选手B获胜&#123;&#125;场比赛，占比&#123;:0.1%&#125;&quot;</span>.<span class="built_in">format</span>(winsB, winsB / n))</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="keyword">def</span> <span class="title function_">main</span>():</span><br><span class="line">    printIntro()    </span><br><span class="line">    probA, probB, n = getIntputs()                <span class="comment"># 获得用户A、B能力值与比赛场次N</span></span><br><span class="line">    winsA, winsB = simNGames(n, probA, probB)     <span class="comment"># 获得A与B的场次</span></span><br><span class="line">    printSummary(winsA, winsB)                    <span class="comment"># 返回A与B的结果</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">main()</span><br></pre></td></tr></table></figure>

<p>（3）测试结果</p>
<p><img src="/pic/python-pic/image-20220712134553584.png" alt="image-20220712134553584"></p>
<h4 id="案例2：斐波那契数列"><a href="#案例2：斐波那契数列" class="headerlink" title="案例2：斐波那契数列"></a>案例2：斐波那契数列</h4><p>自顶向下的方式其实就是<strong>使用递归来求解子问题</strong>，最终解只需要调用递归式，子问题逐步往下层递归的求解。</p>
<p>（1）程序设计：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">cache = &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">fib</span>(<span class="params">number</span>):</span><br><span class="line">    <span class="keyword">if</span> number <span class="keyword">in</span> cache:</span><br><span class="line">        <span class="keyword">return</span> cache[number]</span><br><span class="line">    <span class="keyword">if</span> number == <span class="number">0</span> <span class="keyword">or</span> number == <span class="number">1</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        cache[number] = fib(number - <span class="number">1</span>) + fib(number - <span class="number">2</span>)</span><br><span class="line">    <span class="keyword">return</span> cache[number]</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="built_in">print</span>(fib(<span class="number">35</span>))</span><br></pre></td></tr></table></figure>

<p>（2）运行结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">14930352</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>理解自顶向下的设计思维：分而治之</strong></p>
</blockquote>
<h3 id="逐步组建复杂系统的有效测试方法：自底向上（执行）"><a href="#逐步组建复杂系统的有效测试方法：自底向上（执行）" class="headerlink" title="逐步组建复杂系统的有效测试方法：自底向上（执行）"></a>逐步组建复杂系统的有效测试方法：自底向上（执行）</h3><h4 id="自底向上-模块化集成"><a href="#自底向上-模块化集成" class="headerlink" title="自底向上-模块化集成"></a>自底向上-模块化集成</h4><p>自底向上（执行）就是一种逐步组建复杂系统的有效测试方法。首先将需要解决的问题分为各个三元进行测试，接着按照自顶向下相反的路径进行操作，然后对各个单元进行<strong>逐步组装</strong>，直至系统各部分以组装的思路都经过测试和验证。</p>
<blockquote>
<p><strong>理解自底向上的执行思维：模块化集成</strong></p>
</blockquote>
<p><strong>自底向上分析思想：</strong></p>
<ul>
<li>任何时候栈中符号串和剩余符号串组成一个句型，当句柄出现在栈顶符号串中时，就用该句柄进行归约，这样一直归约到输入串只剩结束符、栈中符号只剩下开始符号，此时认为输入符号串是文法的句子，否则报错。</li>
</ul>
<blockquote>
<p><strong>自底向上是⼀种求解动态规划问题的方法，它不使用递归式，而是直接使用循环来计算所有可能的结果，往上层逐渐累加子问题的解。在求解子问题的最优解的同时，也相当于是在求解整个问题的最优解。其中最难的部分是找到求解最终问题的递归关系式，或者说状态转移方程。</strong></p>
</blockquote>
<h4 id="案例：背包问题"><a href="#案例：背包问题" class="headerlink" title="案例：背包问题"></a>案例：背包问题</h4><p>（1）问题描述</p>
<p>你现在想买⼀大堆算法书，有一个容量为 <strong>V</strong> 的背包，这个商店⼀共有 <strong>n</strong> 个商品。问题在于，你最多只能拿 <strong>W</strong> kg 的东西，其中 <strong>wi</strong> 和 <strong>vi</strong> 分别表示第 <strong>i</strong> 个商品的重量和价值。最终的目标就是在能拿的下的情况下，获得最大价值，求解哪些物品可以放进背包。</p>
<p>对于每⼀个商品你有两个选择：拿或者不拿。</p>
<p>（2）自底向上分析</p>
<p>⾸先要做的就是要找到“子问题”是什么。通过分析发现：每次背包新装进⼀个物品就可以把剩余的承重能力作为⼀个新的背包来求解，⼀直递推到承重为0的背包问题。</p>
<p>用 <strong>m[i,w]</strong> 表示偷到商品的总价值，其中 <strong>i</strong> 表示⼀共多少个商品，<strong>w</strong> 表示总重量，所以求解 m[i,w]就是子问题，那么看到某⼀个商品i的时候，如何决定是不是要装进背包，需要考虑以下：</p>
<ul>
<li>该物品的重量大于背包的总重量，不考虑，换下⼀个商品；</li>
<li>该商品的重量小于背包的总重量，那么尝试把它装进去，如果装不下就把其他东西换出来，看看装进去后的总价值是不是更高了，否则还是按照之前的装法；</li>
<li>极端情况，所有的物品都装不下或者背包的承重能力为0，那么总价值都是0；</li>
</ul>
<p>由以上的分析，可以得出m[i,w]的状态转移方程为：</p>
<blockquote>
<p><strong>m[i,w] &#x3D; max{m[i-1,w], m[i-1,w-wi]+vi}</strong></p>
</blockquote>
<p>（3）程序设计</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 循环的⽅式，自底向上求解</span></span><br><span class="line">cache = &#123;&#125;</span><br><span class="line">items = <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">9</span>)</span><br><span class="line">weights = [<span class="number">10</span>,<span class="number">1</span>,<span class="number">5</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="number">7</span>,<span class="number">3</span>,<span class="number">12</span>,<span class="number">5</span>]</span><br><span class="line">values = [<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">15</span>,<span class="number">40</span>,<span class="number">6</span>,<span class="number">9</span>,<span class="number">12</span>,<span class="number">18</span>]</span><br><span class="line"><span class="comment"># 最⼤承重能⼒</span></span><br><span class="line">W = <span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">knapsack</span>():</span><br><span class="line">    <span class="keyword">for</span> w <span class="keyword">in</span> <span class="built_in">range</span>(W+<span class="number">1</span>):</span><br><span class="line">        cache[get_key(<span class="number">0</span>,w)] = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> items:</span><br><span class="line">        cache[get_key(i,<span class="number">0</span>)] = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> w <span class="keyword">in</span> <span class="built_in">range</span>(W+<span class="number">1</span>):</span><br><span class="line">            <span class="keyword">if</span> w &gt;= weights[i]:</span><br><span class="line">                <span class="keyword">if</span> cache[get_key(i-<span class="number">1</span>,w-weights[i])] + values[i] &gt; cache[get_key(i-<span class="number">1</span>,w)]:</span><br><span class="line">                    cache[get_key(i,w)] = values[i] + cache[get_key(i-<span class="number">1</span>,w-weights[i])]</span><br><span class="line">                <span class="keyword">else</span>:</span><br><span class="line">                    cache[get_key(i,w)] = cache[get_key(i-<span class="number">1</span>,w)]</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                cache[get_key(i,w)] = cache[get_key(i-<span class="number">1</span>,w)]</span><br><span class="line">    <span class="keyword">return</span> cache[get_key(<span class="number">8</span>,W)]</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_key</span>(<span class="params">i,w</span>):</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">str</span>(i)+<span class="string">&#x27;,&#x27;</span>+<span class="built_in">str</span>(w)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="comment"># 背包把所有东西都能装进去做假设开始</span></span><br><span class="line">    <span class="built_in">print</span>(knapsack())</span><br></pre></td></tr></table></figure>

<p>（4）运行结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">29</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://rushiy.gitee.io">rushiy</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://rushiy.gitee.io/python/python%E8%AF%AD%E6%B3%95%E5%8F%8A%E7%BC%96%E7%A8%8B%E6%80%9D%E6%83%B3/">https://rushiy.gitee.io/python/python%E8%AF%AD%E6%B3%95%E5%8F%8A%E7%BC%96%E7%A8%8B%E6%80%9D%E6%83%B3/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://rushiy.gitee.io" target="_blank">如是的学习博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/python/">python</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/favicon.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">rushiy</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">28</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:rushiy@reg.email.cn" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E5%A4%A7%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-number">1.</span> <span class="toc-text">四大基本语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%91%BD%E5%90%8D%E8%A7%84%E5%88%99"><span class="toc-number">1.1.</span> <span class="toc-text">命名规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%A9%E8%BF%9B%E5%8E%9F%E5%88%99"><span class="toc-number">1.2.</span> <span class="toc-text">缩进原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%89%B9%E6%AE%8A%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">1.3.</span> <span class="toc-text">特殊关键字</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%89%B9%E6%AE%8A%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.4.</span> <span class="toc-text">特殊运算符</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#pyhon%E7%BC%96%E7%A8%8B%E6%80%9D%E6%83%B3"><span class="toc-number">2.</span> <span class="toc-text">pyhon编程思想</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E7%9A%84%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F"><span class="toc-number">2.1.</span> <span class="toc-text">基本的程序设计模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3%E5%86%B3%E5%A4%8D%E6%9D%82%E9%97%AE%E9%A2%98%E7%9A%84%E6%9C%89%E6%95%88%E6%96%B9%E6%B3%95%EF%BC%9A%E8%87%AA%E9%A1%B6%E5%90%91%E4%B8%8B%EF%BC%88%E8%AE%BE%E8%AE%A1%EF%BC%89"><span class="toc-number">2.2.</span> <span class="toc-text">解决复杂问题的有效方法：自顶向下（设计）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%87%AA%E9%A1%B6%E5%90%91%E4%B8%8B-%E5%88%86%E8%80%8C%E6%B2%BB%E4%B9%8B"><span class="toc-number">2.2.1.</span> <span class="toc-text">自顶向下-分而治之</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A1%88%E4%BE%8B1%EF%BC%9A%E4%BD%93%E8%82%B2%E7%AB%9E%E6%8A%80%E5%88%86%E6%9E%90"><span class="toc-number">2.2.2.</span> <span class="toc-text">案例1：体育竞技分析</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A1%88%E4%BE%8B2%EF%BC%9A%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97"><span class="toc-number">2.2.3.</span> <span class="toc-text">案例2：斐波那契数列</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%90%E6%AD%A5%E7%BB%84%E5%BB%BA%E5%A4%8D%E6%9D%82%E7%B3%BB%E7%BB%9F%E7%9A%84%E6%9C%89%E6%95%88%E6%B5%8B%E8%AF%95%E6%96%B9%E6%B3%95%EF%BC%9A%E8%87%AA%E5%BA%95%E5%90%91%E4%B8%8A%EF%BC%88%E6%89%A7%E8%A1%8C%EF%BC%89"><span class="toc-number">2.3.</span> <span class="toc-text">逐步组建复杂系统的有效测试方法：自底向上（执行）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%87%AA%E5%BA%95%E5%90%91%E4%B8%8A-%E6%A8%A1%E5%9D%97%E5%8C%96%E9%9B%86%E6%88%90"><span class="toc-number">2.3.1.</span> <span class="toc-text">自底向上-模块化集成</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A1%88%E4%BE%8B%EF%BC%9A%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98"><span class="toc-number">2.3.2.</span> <span class="toc-text">案例：背包问题</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E6%A8%A1%E6%9D%BF%E7%B3%BB%E7%BB%9F/" title="Django模板系统"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django模板系统"/></a><div class="content"><a class="title" href="/django/Django%E6%A8%A1%E6%9D%BF%E7%B3%BB%E7%BB%9F/" title="Django模板系统">Django模板系统</a><time datetime="2022-11-04T07:08:03.000Z" title="发表于 2022-11-04 15:08:03">2022-11-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E8%A7%86%E5%9B%BE/" title="Django视图"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django视图"/></a><div class="content"><a class="title" href="/django/Django%E8%A7%86%E5%9B%BE/" title="Django视图">Django视图</a><time datetime="2022-11-03T07:07:51.000Z" title="发表于 2022-11-03 15:07:51">2022-11-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/Django%E8%B7%AF%E7%94%B1%E7%B3%BB%E7%BB%9F/" title="Django路由系统"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Django路由系统"/></a><div class="content"><a class="title" href="/django/Django%E8%B7%AF%E7%94%B1%E7%B3%BB%E7%BB%9F/" title="Django路由系统">Django路由系统</a><time datetime="2022-11-02T07:07:34.000Z" title="发表于 2022-11-02 15:07:34">2022-11-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/django/%E8%B5%B0%E8%BF%9BDjango/" title="走进Django"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="走进Django"/></a><div class="content"><a class="title" href="/django/%E8%B5%B0%E8%BF%9BDjango/" title="走进Django">走进Django</a><time datetime="2022-11-01T07:01:20.000Z" title="发表于 2022-11-01 15:01:20">2022-11-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/python/python%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97/" title="python常用模块"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="python常用模块"/></a><div class="content"><a class="title" href="/python/python%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97/" title="python常用模块">python常用模块</a><time datetime="2022-10-28T15:34:07.000Z" title="发表于 2022-10-28 23:34:07">2022-10-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By rushiy</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>